using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using HadronData;

using Hadron.SpecialEffects;

namespace Hadron
{
    class EnemyPlasma
        : Projectile
    {
        private ParticleSystem m_particleSystem;
        private bool m_bMissile;
        private double m_dLastBeep;

        public EnemyPlasma(Environment p_environment, bool p_bMissile,
            Vector2 p_vecPosition)
            : base(p_environment) 
        {
            m_bMissile = p_bMissile;
            m_particleSystem = new ParticleSystem(64, 0.0f, 1.0f);
            m_particleSystem.ParticleColours = m_bMissile
                ? new Color[] { Color.Orange, Color.DarkMagenta, Color.Black }
                : new Color[] { Color.Green, Color.DarkOrange, Color.Black };
            m_particleSystem.EmissionVelocityVariance = 16.0f;
            m_particleSystem.EmissionRate = 64.0f;
            m_particleSystem.Position = p_vecPosition;
            m_particleSystem.Active = true;
            m_fLifetime = 10.0f;
            m_circleBounds.Radius = 8.0f;

            AssetManager.Sounds.EnemyProjectile.Play(
                m_environment.ComputeSoundVolume(p_vecPosition));
        }

        public override void Update(GameTime p_gameTime)
        {
            if (m_bMissile)
            {
                Vector2 vecTargetDirection = m_environment.PlayerShip.Position - m_vecPosition;
                Vector2 vecDirection = m_vecVelocity;
                if (Vector2.Dot(vecTargetDirection, vecDirection) > 0.0f)
                {
                    vecTargetDirection.Normalize();
                    vecDirection.Normalize();
                    vecDirection = vecDirection * 4.0f + vecTargetDirection;
                    vecDirection.Normalize();
                    m_vecVelocity = vecDirection * 64.0f;
                }

                // handle proximity beep
                double dTime = p_gameTime.TotalGameTime.TotalSeconds;
                double dBeepInterval
                    = (m_environment.PlayerShip.Position - m_vecPosition).Length() * 0.005f;
                if (dTime - m_dLastBeep > dBeepInterval)
                {
                    AssetManager.Sounds.ProximityBeep.Play(
                        m_environment.ComputeSoundVolume(m_vecPosition));
                    m_dLastBeep = dTime;
                }
            }

            base.Update(p_gameTime);

            m_particleSystem.Position = m_vecPosition;
            m_particleSystem.Update(p_gameTime);

            float fDeltaTime = (float)p_gameTime.ElapsedGameTime.TotalSeconds;
            m_fLifetime -= fDeltaTime;

            if (m_fLifetime <= 0.0f)
            {
                // spawn expiration blast
                ProjectileBlast projectileBlast = new ProjectileBlast(m_environment, Position,
                    m_bMissile ? Color.Orange : Color.YellowGreen);

                projectileBlast.ParticleSystem.EmissionVelocity = m_vecVelocity * 0.1f;
                projectileBlast.ParticleSystem.EmissionVelocityVariance
                    = m_vecVelocity.Length();
                m_environment.SpecialEffects.Add(projectileBlast);
            }
        }

        public override void Draw(SpriteBatch p_spriteBatch)
        {
            p_spriteBatch.Draw(AssetManager.Textures.EnemyProjectile, m_vecPosition, null,
                m_bMissile ? Color.Orange : Color.Green, m_fLifetime, new Vector2(16.0f, 16.0f), 1.0f, SpriteEffects.None, 0.0f);
            m_particleSystem.Draw(p_spriteBatch);
        }

        public override Geometry Geometry
        {
            get
            {
                Vector2 vecDelta = m_vecVelocity;
                vecDelta.Normalize();
                vecDelta *= 8.0f;
                return new Edge(m_vecPosition - vecDelta, m_vecPosition);
            }
        }
    }
}
